package cn.qzuie.smart.module.dorm.controller.admin.energyequipment;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.qzuie.smart.framework.common.pojo.PageParam;
import cn.qzuie.smart.framework.common.pojo.PageResult;
import cn.qzuie.smart.framework.common.pojo.CommonResult;
import cn.qzuie.smart.framework.common.util.object.BeanUtils;
import static cn.qzuie.smart.framework.common.pojo.CommonResult.success;

import cn.qzuie.smart.framework.excel.core.util.ExcelUtils;

import cn.qzuie.smart.framework.apilog.core.annotation.ApiAccessLog;
import static cn.qzuie.smart.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.qzuie.smart.module.dorm.controller.admin.energyequipment.vo.*;
import cn.qzuie.smart.module.dorm.dal.dataobject.energyequipment.EnergyEquipmentDO;
import cn.qzuie.smart.module.dorm.service.energyequipment.EnergyEquipmentService;

@Tag(name = "管理后台 - 能源设备")
@RestController
@RequestMapping("/dorm/energy-equipment")
@Validated
public class EnergyEquipmentController {

    @Resource
    private EnergyEquipmentService energyEquipmentService;

    @PostMapping("/create")
    @Operation(summary = "创建能源设备")
    @PreAuthorize("@ss.hasPermission('dorm:energy-equipment:create')")
    public CommonResult<Long> createEnergyEquipment(@Valid @RequestBody EnergyEquipmentSaveReqVO createReqVO) {
        return success(energyEquipmentService.createEnergyEquipment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新能源设备")
    @PreAuthorize("@ss.hasPermission('dorm:energy-equipment:update')")
    public CommonResult<Boolean> updateEnergyEquipment(@Valid @RequestBody EnergyEquipmentSaveReqVO updateReqVO) {
        energyEquipmentService.updateEnergyEquipment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除能源设备")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dorm:energy-equipment:delete')")
    public CommonResult<Boolean> deleteEnergyEquipment(@RequestParam("id") Long id) {
        energyEquipmentService.deleteEnergyEquipment(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除能源设备")
                @PreAuthorize("@ss.hasPermission('dorm:energy-equipment:delete')")
    public CommonResult<Boolean> deleteEnergyEquipmentList(@RequestParam("ids") List<Long> ids) {
        energyEquipmentService.deleteEnergyEquipmentListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得能源设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dorm:energy-equipment:query')")
    public CommonResult<EnergyEquipmentRespVO> getEnergyEquipment(@RequestParam("id") Long id) {
        EnergyEquipmentDO energyEquipment = energyEquipmentService.getEnergyEquipment(id);
        return success(BeanUtils.toBean(energyEquipment, EnergyEquipmentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得能源设备分页")
    @PreAuthorize("@ss.hasPermission('dorm:energy-equipment:query')")
    public CommonResult<PageResult<EnergyEquipmentRespVO>> getEnergyEquipmentPage(@Valid EnergyEquipmentPageReqVO pageReqVO) {
        PageResult<EnergyEquipmentDO> pageResult = energyEquipmentService.getEnergyEquipmentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EnergyEquipmentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出能源设备 Excel")
    @PreAuthorize("@ss.hasPermission('dorm:energy-equipment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEnergyEquipmentExcel(@Valid EnergyEquipmentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EnergyEquipmentDO> list = energyEquipmentService.getEnergyEquipmentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "能源设备.xls", "数据", EnergyEquipmentRespVO.class,
                        BeanUtils.toBean(list, EnergyEquipmentRespVO.class));
    }

}